$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'test/unit'
require 'model'

class TcOfx < Test::Unit::TestCase
  
  TEST_DIR = 'test_data'
  SAMPLE_DIR = 'sample_data'
  
  def setup
    @ofx = Ofx.new("#{SAMPLE_DIR}/ofx_sample/checking.ofx") if @ofx.nil?
  end
  
  def test_ofx_header
    header = @ofx.header
    assert_equal('100', header.ofx_header)
    assert_equal('OFXSGML', header.data)
    assert_equal('102', header.version)
    assert_equal('NONE', header.security)
    assert_equal('USASCII', header.encoding)
    assert_equal('NONE', header.compression)
    assert_equal('NONE', header.old_file_uid)
    assert_equal('NONE', header.new_file_uid)
  end
  
  def test_ofx_signon_response
    signon = @ofx.signon_response
    assert_equal('0', signon.status_code)
    assert_equal('INFO', signon.status_severity)
    assert_equal(Date.new(2008, 7, 27), signon.response_time)
    assert_equal('ENG', signon.response_language)
    assert_equal('TEST BANK', signon.institution_name)
    assert_equal('TB', signon.institution_id)
  end
 
  def test_ofx_statement
    #transactions?
    stmt = @ofx.statement
    assert_equal('USD', stmt.currency)
    assert_equal('123456789', stmt.account_from_bank_id)
    assert_equal('554433221100', stmt.account_id)
    assert_equal('CHECKING', stmt.account_type)
    assert_equal('7654.32', stmt.ledger_balance)
    assert_equal(Date.new(2008, 7, 25), stmt.ledger_balance_date)
  end
  
  def test_ofx_transactions
    stmt = @ofx.statement
    tx = stmt.transactions[0]
    assert_equal('200807152', tx.transaction_id)
    assert_equal('CHECK', tx.transaction_type)
    assert_equal('-99.99', tx.amount)
    assert_equal(Date.new(2008, 7, 15), tx.date_posted)
    assert_equal('CHECK', tx.name)
    assert_equal('1233', tx.check_number)
    assert_equal(nil, tx.correct_transaction_id)
    assert_equal(nil, tx.correct_action)
  end
  
  def test_ofx_util
    assert_equal(Date.new(2008, 7, 1), OfxUtil.parse_date("20080701120000"))
    assert_raise(RuntimeError) { OfxUtil.parse_date("7/11/2008") }
  end
 
  def test_credit_card
    cc_ofx = Ofx.new("#{SAMPLE_DIR}/ofx_sample/credit_card.ofx")
    #cc header
    assert_equal('102', cc_ofx.header.version)
    #cc signon response
    assert_equal('0', cc_ofx.signon_response.status_code)
    assert_equal('INFO', cc_ofx.signon_response.status_severity)
    assert_equal(Date.new(2008, 7, 27), cc_ofx.signon_response.response_time)
    assert_equal('ENG', cc_ofx.signon_response.response_language)
    assert_equal(nil, cc_ofx.signon_response.institution_name)
    assert_equal(nil, cc_ofx.signon_response.institution_id)
    #cc statement
    assert_equal('USD', cc_ofx.statement.currency)
    assert_equal(nil, cc_ofx.statement.account_from_bank_id)
    assert_equal('9999111122223333', cc_ofx.statement.account_id)
    assert_equal(nil, cc_ofx.statement.account_type)
    assert_equal('-2048.16', cc_ofx.statement.ledger_balance)
    assert_equal(Date.new(2008, 7, 17), cc_ofx.statement.ledger_balance_date)
    #cc transaction
    assert_equal('200807143', cc_ofx.statement.transactions[0].transaction_id)
    assert_equal('CREDIT', cc_ofx.statement.transactions[0].transaction_type)
    assert_equal('12.34', cc_ofx.statement.transactions[0].amount)
    assert_equal(Date.new(2008, 7, 14), cc_ofx.statement.transactions[0].date_posted)
    assert_equal('BIG BOX STORE REFUND', cc_ofx.statement.transactions[0].name)
    assert_equal(nil, cc_ofx.statement.transactions[0].check_number)
    assert_equal('200807143', cc_ofx.statement.transactions[0].correct_transaction_id)
    assert_equal('REPLACE', cc_ofx.statement.transactions[0].correct_action)
  end
end
